/*
 * Copyright @ 2010 Radomir Mladenovic and BugsIO Solutions
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 *       
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.gjutil.caching;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

public class DefaultCacheProvidersRegistry implements CacheProvidersRegistry {

	private CacheProvider defaultCacheProvider;
	private HashMap<String, CacheProvider> caches = new HashMap<String, CacheProvider>();
	private List<CacheProvider> providers = new ArrayList<CacheProvider>();
	
	public void addCacheProvider(CacheProvider cacheProvider, String... caches) {
		for(String cache: caches) {
			this.caches.put(cache, cacheProvider);
			if(!providers.contains(cacheProvider))
				providers.add(cacheProvider);
		}
	}

	public CacheProvider getCacheProvider(String cache) {
		if(cache != null) {
			CacheProvider cacheProvider = caches.get(cache);
			if(cacheProvider != null)
				return cacheProvider;
		}
		return defaultCacheProvider;
	}

	public CacheProvider getDefaultCacheProvider() {
		return defaultCacheProvider;
	}

	public void setDefaultCacheProvider(CacheProvider cacheProvider) {
		defaultCacheProvider = cacheProvider;
		if(!providers.contains(cacheProvider))
			providers.add(cacheProvider);
	}
	
	public Iterable<CacheProvider> getAllCacheProviders() {
		return Collections.unmodifiableCollection(providers);
	}
}
